package ro.myApp.articles.service.publication;

import javax.annotation.PostConstruct;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import org.hibernate.Hibernate;

import ro.myApp.articles.entities.Article;
import ro.myApp.articles.entities.Comment;
import ro.myApp.articles.entities.Publication;
import ro.myApp.articles.entities.Review;
import ro.myApp.articles.service.basic.ArticlesAccessService;

@Remote(value=PublicationAccessService.class)
@Local(value=PublicationAccessServiceLocal.class)
@Stateless
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class PublicationAccessServiceImpl extends ArticlesAccessService<Publication>
	implements PublicationAccessService{

	@Override
	@PostConstruct
	protected void init(){
		clazz = Publication.class;
	}

	@Override
	public Publication findByIdFetchAll(Long publicationId) {
		Publication x = findById(publicationId);
		
		Hibernate.initialize(x.getAuthor());
		Hibernate.initialize(x.getComments());
		
		if (x instanceof Review)
			Hibernate.initialize(((Review)x).getTranslations());
		if (x instanceof Article){
			Hibernate.initialize(((Article)x).getTranslations());
			Hibernate.initialize(((Article)x).getReviews());
		}
		
		return x;
	}
	
}
